var $ = require('../../utils/Ajax.js');
Page({
  data: {
    Search: [],
    SearchCont: '',
    SearchText: 0,
    HotWordT: [],
    HotWordB: []
  },

  onLoad: function () {

  },

  onShow: function () {
    this.setData({
      SearchCont: ""
    })

    var Search = wx.getStorageSync('Search');
    if (Search.length != 0) {
      this.setData({
        Search: Search
      })
    }

    var HotWord = JSON.parse(wx.getStorageSync('HotWord'));
    console.log(HotWord)
    if (HotWord.length < 5){
      this.setData({
        HotWordT: HotWord
      })
    }else{
      this.setData({
        HotWordT: HotWord.slice(0, 4),
        HotWordB: HotWord.slice(4, 7)
      })
    }
    console.log(this.data.HotWordT)
    console.log(this.data.HotWordB)
  },
  
  // 点击搜索文字
  ClickSearch: function(){
    if (this.data.SearchCont == ''){
      wx.showToast({
        title: '搜索内容不能为空',
        icon: 'none',
        duration: 1500
      })
    }else{
      var Search = this.data.Search;
      // 判断搜索记录是否大于十个
      // 如果为真 则取前九个
      if (Search.length >= 10) {
        Search = Search.slice(0,9);
      }
      var SearchCont = this.data.SearchCont;
      Search.unshift(SearchCont);
      this.setData({
        Search: Search
      })
      wx.setStorageSync('Search', this.data.Search);
      wx.navigateTo({
        url: '../SearchCont/SearchCont?SearchCont=' + this.data.SearchCont,
      })
    }
  },

  // 点击取消按钮
  CloseSearch: function(){
    wx.navigateBack({
      delta: 1
    })
  },

  // 点击搜索历史
  ClickSearch_: function(e){
    var SearchCont = e.currentTarget.dataset.seach;
    wx.navigateTo({
      url: '../SearchCont/SearchCont?SearchCont=' + SearchCont,
    })
  },

  // 点击删除搜索记录
  Delect: function(e){
    wx.removeStorageSync('Search');
    this.setData({
      Search: []
    })
  },

  // 点击删除单条搜索记录
  Delect_: function(e){
    var Arr = [];
    var that = this;
    var Index = e.currentTarget.dataset.delectindex;
    var Search = this.data.Search;
    if (Search.length <= 1){
      that.setData({
        Search: Arr
      })
    }else{
      for (var i = 0; i < Search.length; i++) {
        if (i != Index) {
          Arr.push(Search[i]);
          that.setData({
            Search: Arr
          })
          wx.setStorageSync('Search', Arr);
        }
      }
    }
  },

  // 监听搜索框内容
  CompleteSearch: function(e){
    var Search = e.detail.value.replace(/\s+/g,'');
    if (Search.length > 0){
      this.setData({
        SearchCont: Search,
        SearchText: 1
      })
    }else{
      this.setData({
        SearchCont: Search,
        SearchText: 0
      })
    }
  }
})